package com.shadow.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.Serializable;
import java.util.List;
import java.util.Map;

@Repository(value="util")
public class DBUtil{
    private Logger log = LoggerFactory.getLogger(DBUtil.class);

    private SimpleJdbcTemplate simpleJdbcTemplate;

    /**
     * 添加或者更新数据
     * @param sql
     * @param obj
     * @return
     */
    public Integer addOrUpdate(String sql,Object obj){
        Integer id = 0;
        try {
            id = simpleJdbcTemplate.update(sql, new BeanPropertySqlParameterSource(obj));
        } catch (Exception e) {
            log.info(e.toString());
            throw e;
        }
        return id;
    }

    /**
     * 获取List集合
     * @param sql
     * @param className
     * @param obj
     * @return
     */
    @SuppressWarnings("unchecked")
    public List<?> getObjList(String sql,Class<?> className,Object[] obj){

        List<?> array = null;
        try {
            array =  simpleJdbcTemplate.queryForList(sql,
                    ParameterizedBeanPropertyRowMapper.newInstance(className),
                    obj);
        } catch (Exception e) {
            log.info(e.toString());
            throw e;
        }
        return array;
    }

    /**
     * 获取Map集合值
     * @param sql
     * @param obj
     * @return
     */
    public Map<String,?> getMap(String sql,Object[] obj){

        Map<String, ?> map = null;
        try {
            map = simpleJdbcTemplate.queryForMap(sql, obj);
        } catch (Exception e) {
            log.info(e.toString());
            throw e;
        }

        return map;
    }

    /**
     * 获取相应的Object
     * @param sql
     * @param className
     * @param obj
     * @return
     */
    public Object getObject(String sql,Class<? extends Serializable> className,Object[] obj){

        Object object = null;
        try {
            object = simpleJdbcTemplate.queryForObject(sql, BeanPropertyRowMapper.newInstance(className), obj);
        } catch (DataAccessException e) {
            log.info(e.toString());
            throw e;
        }
        return object;
    }

    /**
     * 批量操作
     * @param sql
     * @param obj
     * @return
     */
    public int[] batchOperate(String sql,List<?> obj){

        int[] a = null;
        try {
            a = simpleJdbcTemplate.batchUpdate(sql,
                    SqlParameterSourceUtils.createBatch(obj.toArray()));
        } catch (Exception e) {
            log.info(e.toString());
            throw e;
        }
        return a;
    }

    /**
     * 检查是否有值
     * @param sql
     * @param obj
     * @return
     */
    public int isExist(String sql,Object[] obj){
        int index = 0;
        try {
            index = simpleJdbcTemplate.queryForInt(sql, obj);
        } catch (Exception e) {
            log.info(e.toString());
            throw e;
        }
        return index;
    }

    /**
     * 编辑操作(增删改查都可以)
     * @param sql
     * @param obj
     * @return
     */
    public int editObject(String sql,Object[] obj){
        int index = 0;
        try {
            index = simpleJdbcTemplate.update(sql, obj);
        } catch (DataAccessException e) {
            log.info(e.toString());
            throw e;
        }
        return index;
    }


    @Resource(name="dataSource")
    public void setSimpleJdbcTemplate(DataSource dataSource) {
        this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }
}